package com.example.myapplicationndk.itemtouch;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ValueAnimator;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.DecelerateInterpolator;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.myapplicationndk.R;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import androidx.recyclerview.widget.RecyclerView;


public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ItemViewHolder> implements MyItemTouchHelperCallback.ItemTouchHelperAdapter {

    public Context mContext;
    private List<String> mDatas = new ArrayList<>();
    public static final int ITEM_TYPE_SINGLE_MATERIAL = 0;
    public static final int ITEM_TYPE_ALBUM_TITLE = 1;
    public static final int ITEM_TYPE_ALBUM_ITEM = 2;
    private int lastSelectPos = -1;

    public MyAdapter(Context context, List<String> mdatas) {
        this.mContext = context;
        this.mDatas = mdatas;
    }

    //    @Override
//    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        View view = null;
//        if (viewType == ITEM_TYPE_SINGLE_MATERIAL)
//            view = LayoutInflater.from(mContext).inflate(R.layout.single_material_item, parent, false);
//        else if (viewType == ITEM_TYPE_ALBUM_TITLE)
//            view = LayoutInflater.from(mContext).inflate(R.layout.album_title_item, parent, false);
//        else
//            view = LayoutInflater.from(mContext).inflate(R.layout.album_item, parent, false);
//
//
//        return new ItemViewHolder(view, viewType);
//    }
    @Override
    public ItemViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;

        view = LayoutInflater.from(mContext).inflate(R.layout.album_item, parent, false);


        return new ItemViewHolder(view, viewType);
    }

    @Override
    public void onBindViewHolder(ItemViewHolder holder, int position) {
        int viewType = getItemViewType(position);
        if (viewType == ITEM_TYPE_ALBUM_ITEM) {
            if (position == mDatas.size() - 1)
                holder.mDivider.setVisibility(View.GONE);
            holder.mAlbumTitle.setText(mDatas.get(position - 2));
            if (holder.itemView.getScrollX() != 0) {
                holder.itemView.scrollTo(0, 0);
                holder.mAlbumDeleteIv.setVisibility(View.VISIBLE);
            }
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return ITEM_TYPE_SINGLE_MATERIAL;
        else if (position == 1)
            return ITEM_TYPE_ALBUM_TITLE;
        else
            return ITEM_TYPE_ALBUM_ITEM;

    }

    @Override
    public int getItemCount() {
        if (null == mDatas) {
            return 2;
        }
        return mDatas.size() + 2;
    }

    @Override
    public void onItemMove(int fromPosition, int toPosition) {
        Collections.swap(mDatas, fromPosition, toPosition);
        notifyItemMoved(fromPosition, toPosition);
    }

    @Override
    public void onItemDelete(int position) {
        mDatas.remove(position);
        notifyItemRemoved(position);
    }

    @Override
    public void onItemChange(int lastSelectPos) {
        notifyItemChanged(lastSelectPos);
    }

    @Override
    public int getLastSelectItem() {
        return lastSelectPos;
    }


    class ItemViewHolder extends RecyclerView.ViewHolder implements MyItemTouchHelperCallback.ItemTouchHelperViewHolder {

        ImageView mAlbumIcon;
        TextView mAlbumTitle;
        ImageView mAlbumDeleteIv;
        TextView mAlbumDeleteTv;
        TextView mDivider;

        ImageView mSingleMaterialGoIv;

        int mAnimDistance;
        ValueAnimator mAnimator;
        int mDirection = -1;


        public ItemViewHolder(View itemView, int type) {
            super(itemView);
            initView(type);
            initListener(type);
        }

        private void initView(int type) {
            if (type == ITEM_TYPE_SINGLE_MATERIAL) {
                mSingleMaterialGoIv = itemView.findViewById(R.id.go_to_single_material_iv);
            } else if (type == ITEM_TYPE_ALBUM_ITEM) {
                mAlbumIcon = itemView.findViewById(R.id.album_icon_iv);
                mAlbumTitle = itemView.findViewById(R.id.album_title_tv);
                mAlbumDeleteIv = itemView.findViewById(R.id.album_delete_iv);
                mAlbumDeleteTv = itemView.findViewById(R.id.album_delete_tv);
                mDivider = itemView.findViewById(R.id.divider);

            }
        }

        public void startAnimation(int direction) {
            mDirection = direction;
            mAnimator.start();

        }

        private void initListener(int type) {
            if (type == ITEM_TYPE_ALBUM_ITEM) {
                mAnimator = ValueAnimator.ofFloat(0.0f, 1.0f);
                mAnimator.setInterpolator(new DecelerateInterpolator());
                mAnimator.setDuration(200);
                mAnimator.addUpdateListener(animation -> {
                    float ratio = animation.getAnimatedFraction();
                    if (mDirection == 1)
                        itemView.scrollTo((int) (ratio * mAnimDistance), 0);
                    else
                        itemView.scrollTo((int) ((1 - ratio) * mAnimDistance), 0);
                });
                mAnimator.addListener(new AnimatorListenerAdapter() {
                                          @Override
                                          public void onAnimationEnd(Animator animation) {
                                              if (mDirection == -1)
                                                  mAlbumDeleteIv.setVisibility(View.VISIBLE);
                                          }

                                          @Override
                                          public void onAnimationStart(Animator animation) {
                                              if (mAnimDistance <= 0)
                                                  mAnimDistance = mAlbumDeleteTv.getWidth();
                                              if (mDirection == 1)
                                                  mAlbumDeleteIv.setVisibility(View.GONE);
                                          }
                                      }
                );
                mAlbumDeleteIv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (lastSelectPos >= 0 && lastSelectPos != getAdapterPosition()) {
                            notifyItemChanged(lastSelectPos);
                        }
                        startAnimation(1);
                        lastSelectPos = getAdapterPosition();
                    }
                });
                mAlbumDeleteTv.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onItemDelete(getAdapterPosition());
                    }
                });

            }
        }

        @Override
        public void onItemSelect() {
            //恢复上一次选中的Item
            if (lastSelectPos != getAdapterPosition()) {
                onItemChange(lastSelectPos);
            }
        }

        @Override
        public void onItemClear() {
            lastSelectPos = getAdapterPosition();
        }
    }
}